<?php

namespace App\Services\ApplyGoldCoinWithdraw;

use App\Models\Withdraw;
use Illuminate\Support\Facades\DB;

class BackFailedWithdrawCoins
{
	/**
	 * @var Withdraw
	 */
	protected $withdraw;

	protected function setParams(Withdraw $withdraw)
	{
		$this->withdraw = $withdraw;
	}

	protected function handle()
	{
		DB::transaction(function () {
			$withdraw = $this->withdraw;
			$user = $withdraw->user;
			$data = ['withdraw_id' => $withdraw->id];
			account_balances($user)->plusFailedWithdrawPrincipal($withdraw->actual_amount, $data);
			account_balances($user)->plusFailedWithdrawCharge($withdraw->charge_amount, $data);
			$withdraw->update(['status' => 'returned']);
		});
	}

	public function back(Withdraw $withdraw)
	{
		$this->setParams($withdraw);
		$this->handle();
	}
}
